package com.canaan.business.service.storehouse.impl;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ArrayUtil;
import com.canaan.business.domain.storehouse.WmsStorehouseStock;
import com.canaan.business.service.storehouse.IWmsStorehouseStockService;
import com.canaan.common.utils.DateUtils;
import com.canaan.common.utils.SecurityUtils;
import com.canaan.common.utils.TableNoUtil;
import com.canaan.common.utils.StringUtils;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTOleObject;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.canaan.business.mapper.storehouse.StockCheckResultMapper;
import com.canaan.business.domain.storehouse.StockCheckResult;
import com.canaan.business.service.storehouse.IStockCheckResultService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 库存盘点结果Service业务层处理
 *
 * @author ruoyi
 * @date 2023-02-16
 */
@Service
public class StockCheckResultServiceImpl implements IStockCheckResultService {
    @Resource
    private StockCheckResultMapper stockCheckResultMapper;
    @Resource
    private IWmsStorehouseStockService storehouseStockService;

    /**
     * 查询库存盘点结果
     *
     * @param id 库存盘点结果主键
     * @return 库存盘点结果
     */
    @Override
    public StockCheckResult selectStockCheckResultById(Long id) {
        return stockCheckResultMapper.selectStockCheckResultById(id);
    }

    /**
     * 查询库存盘点结果列表
     *
     * @param stockCheckResult 库存盘点结果
     * @return 库存盘点结果
     */
    @Override
    public List<StockCheckResult> selectStockCheckResultList(StockCheckResult stockCheckResult) {
        return stockCheckResultMapper.selectStockCheckResultList(stockCheckResult);
    }

    /**
     * 新增库存盘点结果
     *
     * @param stockCheckResult 库存盘点结果
     * @return 结果
     */
    @Override
    public int insertStockCheckResult(StockCheckResult stockCheckResult) {
        if(StringUtils.isBlank(stockCheckResult.getNo())){
            stockCheckResult.setNo(TableNoUtil.getNo(StockCheckResult.class));
        }
        stockCheckResult.setCreateBy(SecurityUtils.getUsername());
        stockCheckResult.setCreateTime(DateUtils.getNowDate());
        return stockCheckResultMapper.insertStockCheckResult(stockCheckResult);
    }

    /**
     * 批量新增库存盘点结果
     *
     * @param stockCheckResultList 库存盘点结果列表
     * @return 结果
     */
    @Override
    public int batchInsertStockCheckResult(List<StockCheckResult> stockCheckResultList) {
        String username = SecurityUtils.getUsername();
        Date currerTime = DateUtils.getNowDate();
        for (StockCheckResult stockCheckResult : stockCheckResultList){
            if(StringUtils.isBlank(stockCheckResult.getNo())){
                stockCheckResult.setNo(TableNoUtil.getNo(StockCheckResult.class));
            }
            stockCheckResult.setCreateBy(username);
            stockCheckResult.setCreateTime(currerTime);
        }
        int insertNum = 0;
        for (int i=0; i<stockCheckResultList.size();) {
            int endIndex = i+500;
            if (endIndex > stockCheckResultList.size()) {
                endIndex = stockCheckResultList.size();
            }
            insertNum = insertNum + stockCheckResultMapper.batchInsertStockCheckResult(stockCheckResultList.subList(i, endIndex));
            i = endIndex;
        }
        return insertNum;
    }

    /**
     * 修改库存盘点结果
     *
     * @param stockCheckResult 库存盘点结果
     * @return 结果
     */
    @Override
    public int updateStockCheckResult(StockCheckResult stockCheckResult) {
        stockCheckResult.setUpdateBy(SecurityUtils.getUsername());
        stockCheckResult.setUpdateTime(DateUtils.getNowDate());
        return stockCheckResultMapper.updateStockCheckResult(stockCheckResult);
    }

    /**
     * 批量修改库存盘点结果
     *
     * @param stockCheckResultList 库存盘点结果列表
     * @return 结果
     */
    @Override
    public int batchUpdateStockCheckResult(List<StockCheckResult> stockCheckResultList) {
        String username = SecurityUtils.getUsername();
        Date currerTime = DateUtils.getNowDate();
        for (StockCheckResult stockCheckResult : stockCheckResultList){
            stockCheckResult.setUpdateBy(username);
            stockCheckResult.setUpdateTime(currerTime);
        }
        int updateNum = 0;
        for (int i=0; i<stockCheckResultList.size();) {
            int endIndex = i+500;
            if (endIndex > stockCheckResultList.size()) {
                endIndex = stockCheckResultList.size();
            }
            updateNum = updateNum + stockCheckResultMapper.batchUpdateStockCheckResult(stockCheckResultList.subList(i, endIndex));
            i = endIndex;
        }
        return updateNum;
    }

    /**
     * 批量删除库存盘点结果
     *
     * @param ids 需要删除的库存盘点结果主键集合
     * @return 结果
     */
    @Override
    public int deleteStockCheckResultByIds(Long[] ids) {
        return stockCheckResultMapper.deleteStockCheckResultByIds(ids);
    }

    /**
     * 删除库存盘点结果信息
     *
     * @param id 库存盘点结果主键
     * @return 结果
     */
    @Override
    public int deleteStockCheckResultById(Long id) {
        return stockCheckResultMapper.deleteStockCheckResultById(id);
    }

    /**
     * 查询库存盘点结果
     *
     * @param no 编号
     * @return 库存盘点结果
     */
    @Override
    public StockCheckResult selectStockCheckResultByNo(String no) {
        return stockCheckResultMapper.selectStockCheckResultByNo(no);
    }

    /**
     * 批量删除库存盘点结果
     *
     * @param nos 需要删除的编号集合
     * @return 结果
     */
    @Override
    public int deleteStockCheckResultByNos(String[] nos) {
        return stockCheckResultMapper.deleteStockCheckResultByNos(nos);
    }

    /**
     * 删除库存盘点结果信息
     *
     * @param no 编号
     * @return 结果
     */
    @Override
    public int deleteStockCheckResultByNo(String no) {
        return stockCheckResultMapper.deleteStockCheckResultByNo(no);
    }



    @Override
    public List<StockCheckResult> selectStockCheckResultByCheckDetailNo(String detailNo) {
        return stockCheckResultMapper.selectStockCheckResultByCheckDetailNo(detailNo);
    }

    @Override
    @Transactional
    public int batchAdd(String checkDetailNo,List<StockCheckResult> stockCheckResults) {
        List<StockCheckResult> stockCheckResults1 = this.selectStockCheckResultByCheckDetailNo(checkDetailNo);
        List<Long> idList = stockCheckResults1.stream().map(e -> e.getId()).collect(Collectors.toList());

        List<StockCheckResult> addList = stockCheckResults.stream().filter(e -> e.getId() == null).collect(Collectors.toList());
        if (CollUtil.isNotEmpty(addList)){
            addList.stream().forEach(e->{
                WmsStorehouseStock storehouseStock = this.storehouseStockService.selectWmsStorehouseStockListByBarcode(e.getBarcode());
                if (storehouseStock != null) {
                    e.setLocationNo(storehouseStock.getStorehouseLocationNo());
                    e.setStorehouseNo(storehouseStock.getStorehouseNo());
                    e.setAreaNo(storehouseStock.getStorehouseAreaNo());
                    e.setProductUnit(storehouseStock.getCompany());
                }
            });
            this.batchInsertStockCheckResult(addList);
            idList.removeAll(addList.stream().map(e->e.getId()).collect(Collectors.toList()));
        }
        List<StockCheckResult> updateList = stockCheckResults.stream().filter(e -> e.getId() != null).collect(Collectors.toList());
        if (CollUtil.isNotEmpty(updateList)){
            this.batchUpdateStockCheckResult(updateList);
            idList.removeAll(updateList.stream().map(e->e.getId()).collect(Collectors.toList()));
        }
        if (CollUtil.isNotEmpty(idList)){
            this.deleteStockCheckResultByIds(ArrayUtil.toArray(idList,Long.class));
        }
        return 1;
    }
}
